package com.neulogistics.contrib.usercenter.api.application.command.handler;

import com.blackto.lib.mediat.unicast.IRequestHandler;
import com.neulogistics.contrib.usercenter.api.application.command.CreateUserCommand;
import com.neulogistics.contrib.usercenter.domain.model.aggregate.User;
import com.neulogistics.contrib.usercenter.domain.model.enumeration.UserRole;
import com.neulogistics.contrib.usercenter.domain.repository.IUserRepository;
import com.neulogistics.infrastructure.common.serviceresult.BaseResponse;
import com.neulogistics.infrastructure.common.serviceresult.ResultUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class CreateUserCommandHandler implements IRequestHandler<CreateUserCommand, BaseResponse<String>> {
    @Resource
    private IUserRepository userRepository;

    @Override
    public BaseResponse<String> handle(CreateUserCommand command) {
        // 创建用户聚合根
        UserRole userRole = UserRole.fromValue(command.userRole, UserRole.class);
        User user = new User(command.username, command.userGender, command.userIdentity,
                command.userPhone, command.name, command.departmentId, command.userAvatar,
                command.departmentName, userRole);

        // 持久化保存
        userRepository.save(user);
        return ResultUtils.success(user.getUserId().toString());
    }
}
